package cn.fjy.android.phone.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Vibrator;
import android.telephony.TelephonyManager;
import android.util.Log;
import cn.fjy.android.phone.worker.Recorder;
import cn.fjy.android.phone.worker.TalkTimeRemind;
import cn.fjy.android.res.Constant;
import cn.fjy.android.utils.APPSession;

/**
 *@author GrayF
 *2011-5-28
 *@version 1.0
 */
public class PhoneStateReceiver extends BroadcastReceiver{
	private final static String tag = "PhoneStateReceiver";

	private int PrePhoneState = TelephonyManager.CALL_STATE_IDLE;
	
	private Vibrator mVibrator;
	
	private TelephonyManager telManager;
	
	private SharedPreferences pre;
	
	private TalkTimeRemind mRemind;
	@Override
	public void onReceive(Context context, Intent intent) {
		mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
		pre = context.getSharedPreferences(Constant.CONFIG_FILE_NAME, Context.MODE_PRIVATE);
		telManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
		onCallStateChanged(telManager.getCallState());
	}
	public void onCallStateChanged(int state) {
		Log.i(tag,"电话状态监听---上一个电话状态："+PrePhoneState);
		checkState(state);
		switch (state) {
		case TelephonyManager.CALL_STATE_IDLE:
			PrePhoneState = TelephonyManager.CALL_STATE_IDLE;
			Log.i(tag, "TelephonyManager.CALL_STATE_IDLE");
			break;
		case TelephonyManager.CALL_STATE_OFFHOOK:
			PrePhoneState = TelephonyManager.CALL_STATE_OFFHOOK;
			Log.i(tag, " TelephonyManager.CALL_STATE_OFFHOOK");
			break;
		case TelephonyManager.CALL_STATE_RINGING:
			PrePhoneState = TelephonyManager.CALL_STATE_RINGING;
			Log.i(tag, "TelephonyManager.CALL_STATE_RINGING");
			break;
		default:
			break;
		}
	}
	private void checkState(int state) {
		// 挂机
		if (PrePhoneState == TelephonyManager.CALL_STATE_OFFHOOK) {
			if (state == TelephonyManager.CALL_STATE_IDLE) {
				Log.i(tag,
						"挂机，checkState##CALL_STATE_OFFHOOK___TO___CALL_STATE_IDLE");
				if(pre.getBoolean(Constant.PROP_KEY_CALL_OFF_VIBRATOR, false)){
					Log.i(tag,"挂机震动");
					mVibrator.vibrate(pre.getInt(Constant.PROP_KEY_CALL_OFF_VIBRATOR_TIME, 100));
				}else{
					Log.i(tag,"未启用挂机震动");
				}
				//关闭来电通话时间提醒
				if(mRemind!=null){
					mRemind.setEnable(false);
					mRemind = null;
				}
				if(pre.getBoolean(Constant.PROP_KEY_RECORD_IN, false)||pre.getBoolean(Constant.PROP_KEY_RECORD_OUT, false)){
					Recorder recorder = (Recorder)APPSession.getValue(Constant.SESSION_KEY_RECORD);
					if(recorder!=null){
						Log.i(tag,"关闭录音");
						recorder.stop();
						APPSession.remove(Constant.SESSION_KEY_RECORD);
					}
				}
			}
		}

		// 来电接通
		if (PrePhoneState == TelephonyManager.CALL_STATE_RINGING) {
			if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
				Log.i(tag,
						"来电接通，checkState##CALL_STATE_RINGING___TO___CALL_STATE_OFFHOOK");
				//来电通话时间提醒
				if(pre.getBoolean(Constant.PROP_KEY_TALKTIME_REMIND, false)){
					mRemind = new TalkTimeRemind(mVibrator,pre.getInt(Constant.PROP_KEY_TALKTIME_REMIND_TYPE,1),telManager);
					mRemind.setEnable(true);
					new Thread(mRemind).start();
				}
				//通话录音
				if(pre.getBoolean(Constant.PROP_KEY_RECORD_IN, false)){
					Recorder recorder = (Recorder)APPSession.getValue(Constant.SESSION_KEY_RECORD);
					if(recorder==null){
						recorder = new Recorder(System.currentTimeMillis()+"");
						APPSession.setValue(Constant.SESSION_KEY_RECORD, recorder);
						new Thread(recorder).start();
					}
				}
			}
		}
	}	
}

